package entity.zhujie.lianxi.demo01;

import java.lang.reflect.Field;

public class Student {
    private String name ;
    @StudentAnnotaion(20)
    private int age ;
    public Student (){
        super();
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age)throws Exception {
         check(this.getClass(),age);
    }

    public Student(String name, int age) throws Exception {
        this.name = name;
        check(this.getClass(),age);
    }

    private static void check(Class<?> clazz,int age) throws Exception {
        Class<Student> aCall = Student.class;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields){
            if (field.isAnnotationPresent(StudentAnnotaion.class)){
                StudentAnnotaion annotation = field.getAnnotation(StudentAnnotaion.class);
                int value = annotation.value();
                if (value > age){
                    System.out.println("大于" + value);
                }else {
                    throw new Exception("年龄小于18");
                }
//                System.out.println(value);
            }
        }
    }
}
